module PrismBreak {

    export class Event {

        public Name: EventType;

        //Subscriptions is a sorted array based on priority
        //The first subscription has the highest priority

        public Subscriptions: Subscription[];

        constructor(name: EventType) {

            this.Subscriptions = [];
            this.Name = name;

        }

        public AddSubscription(sub: Subscription): void {

            var index = (<any>this.Subscriptions).binarySearch(sub.Priority, "Priority");
            index -= 1;

            if (!index || index < 0) {

                index = 0;
            }

            this.Subscriptions.splice(index, 0, sub);
        }

        public RemoveSubscription(token: string): void {

            var index: number;

            var sub: Subscription = Enumerable.From(this.Subscriptions)
                .Where((item: Subscription, i: number) => {
                    if (item.Token === token) {
                        index = i;
                        return true;
                    };
                }).SingleOrDefault(false);


            if (!sub) {

                console.log("Tried to remove a subscription that doesn't exist");
            }
            else {

                this.Subscriptions.splice(index, 1);
            }
        }
    }
}